﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using TTMain.UI;

namespace Thurn_und_Taxis
{
    public delegate void UpdateInterfaceCallback();
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private GameUI _gameUI;        
        private ServerForm serverForm;        
        private SettingsForm settingForm;
        private GameForm gameForm;

        public Window1()
        {
            InitializeComponent();
            serverForm = null;            
            settingForm = null;

            _gameUI = new GameUI();
            _gameUI.CreatedRoomAck += new CreatedRoomAckHandler(_gameUI_CreatedRoomAck);
            _gameUI.JoinedTableAck += new JoinedTableAckHandler(_gameUI_JoinedTableAck);
        }

        void _gameUI_JoinedTableAck(object sender, Int32 roomId, List<String> playerList, List<Boolean> statesList)
        {
            try
            {

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        if (serverForm != null)
                        {                            
                            serverForm.Close();
                            serverForm = null;
                            gameForm = new GameForm(_gameUI, false, roomId);
                            this.Hide();
                            gameForm.Show();
                            gameForm.ReceivedPlayerList(playerList, statesList);
                            gameForm.Closing += new System.ComponentModel.CancelEventHandler(gameForm_Closing);
                        }

                    }
                );
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        void _gameUI_CreatedRoomAck(object sender, int roomId)
        {
            try
            {

                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    (UpdateInterfaceCallback)delegate()
                    {
                        if (serverForm != null)
                        {
                            serverForm.Close();
                            serverForm = null;                            
                            gameForm = new GameForm(_gameUI, true, roomId);
                            gameForm.Closing +=new System.ComponentModel.CancelEventHandler(gameForm_Closing);
                            this.Hide();
                            gameForm.Show();
                        }
                    }
                );
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        void serverForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Show();
            serverForm = null;
        }

        void settingForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Show();
            settingForm = null;
        }

        void gameForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.Show();
            gameForm = null;
        }

        private void btnStartNewGame_Click(object sender, RoutedEventArgs e)
        {
            if (serverForm == null)
            {
                serverForm = new ServerForm(_gameUI);
                serverForm.Show();
                serverForm.Closing += new System.ComponentModel.CancelEventHandler(serverForm_Closing);
                this.Hide();
            }
            else
            {
                serverForm.Show();
            }
        }
        
        private void btnSettings_Click(object sender, RoutedEventArgs e)
        {
            if (settingForm == null)
            {
                settingForm = new SettingsForm(_gameUI.GetGameSettings());
                settingForm.Show();
                settingForm.Closing += new System.ComponentModel.CancelEventHandler(settingForm_Closing);
                this.Hide();
            }
            else
            {
                settingForm.Show();
            }
        }
                
        private void Window_Closed(object sender, EventArgs e)
        {
            if (_gameUI != null)            
                _gameUI.CloseLocalConnection();
        }

        private void btnHelp_Click(object sender, RoutedEventArgs e)
        {
            HelpForm help = new HelpForm();
            help.Show();
            this.Close();
        }

        private void btnQuit_Click(object sender, RoutedEventArgs e)
        {
            if (_gameUI != null)
                _gameUI.CloseLocalConnection();
            serverForm = null;
            settingForm = null;
            this.Close();
        }
    }
}
